package com.skillworld.webapp.model.lesson;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Filter;

import com.skillworld.webapp.model.course.Course;
import com.skillworld.webapp.model.tutorial.Tutorial;

@Entity
@Table(name="Lesson")
public class Lesson
{   
    private Long lessonId;
    private Course course;
	private String name;
	private String description; 
	private List<Tutorial> tutorials = new ArrayList<Tutorial>();
	private List<Exam> exams = new ArrayList<Exam>();

	
	protected Lesson()
	{ 

	}

    public Lesson(Course course, String name, String description)
    {
        this.course = course;
		this.name = name;
		this.description = description;
	}
    
    public Lesson(Course course, String name, String description,
                    List<Tutorial> tutorials)
    {
        this.course = course;
        this.name = name;
        this.description = description;
        this.tutorials = tutorials;
    }
    

	@SequenceGenerator(name="LessonIdGenerator",sequenceName="LessonSeq")
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO,generator="LessonIdGenerator")
	@Column(name="idLesson")
	public Long getLessonId()
	{
        return lessonId;
    }
    
    protected void setLessonId(Long lessonId)
    {
        this.lessonId = lessonId;
    }  
    
    @ManyToOne(optional=false,fetch=FetchType.LAZY)
    @JoinColumn(name="idCourse")
    public Course getCourse()
    {
        return course;
    }
    
    public void setCourse(Course course)
    {
        this.course = course;
    }
    
    @Column(name="name")
    public String getName()
    {
        return name;
    }
    
    public void setName(String name) 
    {
        this.name = name;
    }

    @Column(name="description")
    public String getDescription() 
    {
        return description;
    }
    
    public void setDescription(String description)
    {
        this.description = description;
    }
    
    @ManyToMany
    @JoinTable(name="LessonTutorial",
            joinColumns=@JoinColumn(name="idLesson"),
            inverseJoinColumns=@JoinColumn(name="idTutorial"))
    @OrderBy
    public List<Tutorial> getTutorials()
    {
        return tutorials;
    }
    
    protected void setTutorials(List<Tutorial> tutorials)
    {
        this.tutorials = tutorials;
    }

    @OneToMany(mappedBy="lesson",orphanRemoval=true)
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.DELETE})
    @Filter(name="enabled",condition="enabled")
    @OrderBy
	public List<Exam> getExams()
	{
        return exams;
    }
    
	protected void setExams(List<Exam> exams)
	{
        this.exams = exams;
    }
}
